import { defineStore } from "pinia";
import { OrderApi } from "@/api";
import { ref, computed } from 'vue'


export default defineStore('order', () => {
    const isInit = ref<boolean>(false)
    const allList = ref<Vm.Order[]>([])
    const payList = ref<Vm.Order[]>([])
    const unpayList = ref<Vm.Order[]>([])


    const init = async () => {
        if (isInit.value) return
        isInit.value = true
        allList.value = await OrderApi.getListAll()
        payList.value = await OrderApi.getListPay()
        unpayList.value = await OrderApi.getListUnpay()
    }

    const remove = async (id: string) => {
        await OrderApi.remove(id)
        const n = payList.value.findIndex((item: Vm.Order) => item.orderId === id)
        const m = allList.value.findIndex((item: Vm.Order) => item.orderId === id)
        payList.value.splice(n, 1)
        allList.value.splice(m, 1)
    }
    const update = () => {
        isInit.value = false
    }
    return { init, isInit, allList, payList, unpayList, remove, update }
}, {
    persist: {
        enabled: true
    }
})